package com.atcumt.Write.deadlock;

/**
 * 缩小锁的范围
 */
public class DeadLockTest1 {

    static final Object a = new Object();
    static final Object b = new Object();
    static Thread t1, t2;

    public static void main(String[] args) {
        t1 = new Thread(() -> {
            synchronized (a) {
                System.out.println("in t1-locka");
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            synchronized (b) {
                System.out.println("in t1-lockb");
            }
        });

        t2 = new Thread(() -> {
            synchronized (b) {
                System.out.println("in t2-lockb");
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            synchronized (a) {
                System.out.println("in t2-locka");
            }
        });

        t1.start();
        t2.start();
    }
}
